﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using Microsoft.ApplicationBlocks.Data;

namespace TIS.DAO
{
    public class PartnerDAO
    {
        private static readonly String Url = ConfigurationManager.AppSettings["Main.ConnectionString"];
        public PartnerDAO()
        {

        }

        private SqlConnection _connection;
        private SqlConnection GetConnection()
        {
            _connection = new SqlConnection(Url);
            if (_connection.State == ConnectionState.Closed)
            {
                _connection.Open();
            }

            return _connection;
        }

        public DataTable GetPartner()
        {
            try
            {
                _connection = GetConnection();
                DataSet ds = SqlHelper.ExecuteDataset(_connection, CommandType.Text, "select * from [partner] Where partnerstatus = 1 order by partnerposition");
                return ds.Tables[0];
            }
            finally
            {
                _connection.Close();
            }
        }
        public DataTable GetPartnerAll()
        {
            try
            {
                _connection = GetConnection();
                DataSet ds = SqlHelper.ExecuteDataset(_connection, CommandType.Text, "select * from [partner]");
                return ds.Tables[0];
            }
            finally
            {
                _connection.Close();
            }
        }
        public DataTable GetPartnerById(int id)
        {
            try
            {
                _connection = GetConnection();
                SqlParameter[] spr = new SqlParameter[1];
                spr[0] = new SqlParameter("@PartnerId", id);
                DataSet ds = SqlHelper.ExecuteDataset(_connection, CommandType.Text, "select * from [partner] WHERE @PartnerId = PartnerId",spr);
                return ds.Tables[0];
            }
            finally
            {
                _connection.Close();
            }
        }

        public void DeletePartner(int id)
        {
            try
            {
                _connection = GetConnection();
                SqlParameter[] spr = new SqlParameter[1];
                spr[0] = new SqlParameter("@PartnerId", id);
                SqlHelper.ExecuteNonQuery(_connection, CommandType.Text, "DELETE FROM [partner] WHERE PartnerId = @PartnerId", spr);

            }
            finally
            {
                _connection.Close();
            }
        }

        public void InsertPartner(string name, string url, string filePath, int position, int status)
        {
            try
            {
                _connection = GetConnection();
                SqlParameter[] spr = new SqlParameter[5];
                spr[0] = new SqlParameter("@PartnerName", name);
                spr[1] = new SqlParameter("@PartnerLink", url);
                spr[2] = new SqlParameter("@PartnerImage", filePath);
                spr[3] = new SqlParameter("@PartnerPosition", position);
                spr[4] = new SqlParameter("@PartnerStatus", status);

                SqlHelper.ExecuteNonQuery(_connection, CommandType.Text,
                    "INSERT Partner(PartnerName,PartnerLink,PartnerImage,PartnerPosition,PartnerStatus) VALUES(@PartnerName,@PartnerLink,@PartnerImage,@PartnerPosition,@PartnerStatus)", spr);

            }
            finally
            {
                _connection.Close();
            }
        }

        public void UpdatePartner(int id,string name, string url, string filePath, int position, int status)
        {
            try
            {
                _connection = GetConnection();
                if (filePath != "")
                {
                    SqlParameter[] spr = new SqlParameter[6];
                    spr[0] = new SqlParameter("@PartnerName", name);
                    spr[1] = new SqlParameter("@PartnerLink", url);
                    spr[2] = new SqlParameter("@PartnerImage", filePath);
                    spr[3] = new SqlParameter("@PartnerPosition", position);
                    spr[4] = new SqlParameter("@PartnerStatus", status);
                    spr[5] = new SqlParameter("@PartnerId", id);
                    SqlHelper.ExecuteNonQuery(_connection, CommandType.Text,
                        "UPDATE Partner SET PartnerName = @PartnerName,PartnerLink = @PartnerLink,PartnerImage = @PartnerImage,PartnerPosition = @PartnerPosition,PartnerStatus = @PartnerStatus WHERE PartnerId = @PartnerId", spr);
                }
                else
                {
                    SqlParameter[] spr = new SqlParameter[5];
                    spr[0] = new SqlParameter("@PartnerName", name);
                    spr[1] = new SqlParameter("@PartnerLink", url);
                    spr[2] = new SqlParameter("@PartnerPosition", position);
                    spr[3] = new SqlParameter("@PartnerStatus", status);
                    spr[4] = new SqlParameter("@PartnerId", id);
                    SqlHelper.ExecuteNonQuery(_connection, CommandType.Text,
                        "UPDATE Partner SET PartnerName = @PartnerName,PartnerLink = @PartnerLink,PartnerPosition = @PartnerPosition,PartnerStatus = @PartnerStatus WHERE PartnerId = @PartnerId", spr);
                }


            }
            finally
            {
                _connection.Close();
            }
        }

    }
}